package com.nondo.dean.product.controller.api.admin;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.ValidateUtil;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.product.domain.Series;
import com.nondo.dean.product.service.SeriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author ken.
 * @version 1.0
 * @email 695093513@qq.com
 * @date 2018/3/28
 */
@RestController("apiAdminSeries")
@RequestMapping("/{v}/admin/series")
public class SeriesController {
    @Autowired
    SeriesService seriesService;

    /**
     * 列表
     *
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<Page<Series>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<Series> query) {
        View<Page<Series>> view = new View<Page<Series>>(20000, "获取数据成功");

        Series series = query.getData();

        Page<Series> page = new Page<Series>(query.getPage(), query.getLimit(), seriesService.count(series));

        List<Series> gradeList = seriesService.getList(query.getOffset(), query.getLimit(), series);

        page.setContent(gradeList);

        view.setData(page);

        return view;
    }

    /**
     * 添加
     *
     * @param request
     * @param response
     * @param series
     * @param result
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/add")
    public View<Series> add(HttpServletRequest request, HttpServletResponse response, @Validated @RequestBody Series series, BindingResult result) {
        View view = new View(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            //检查是否存在重复的数据
            Series seriesCheck = this.seriesService.selectByName(series.getName());
            if (seriesCheck != null) {
                return View.of401Error("该系列已经存在，请不要重复添加");
            }
            this.seriesService.add(series);
        }

        return view;
    }

    /**
     * 更新
     *
     * @param request
     * @param response
     * @param series
     * @param result
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/update")
    public View<Series> update(HttpServletRequest request, HttpServletResponse response, @Validated @RequestBody Series series, BindingResult result) {
        View view = new View(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            Series seriesCheck = this.seriesService.selectByName(series.getName());
          /*  if (seriesCheck != null && series.getId().equals(seriesCheck.getId())) {
                return View.ofParamsError("已经存在该系列名字，无法修改为相同的名字");
            }*/

            this.seriesService.update(series);
        }

        return view;
    }

    /**
     * 删除
     *
     * @param request
     * @param response
     * @param seriesId
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/delete/{seriesId}")
    public View<Series> delete(HttpServletRequest request, HttpServletResponse response, @PathVariable int seriesId) {
        View view = new View(20000, "删除成功");

        Series series = this.seriesService.select(seriesId);

        //如果系列已经被产品关联，则无法被删除
        int checkProduct = this.seriesService.checkProduct(series);
        if (checkProduct > 0) {
            return View.of401Error("该系列已经被产品关联，无法被删除，请删除对应产品关联后方可删除该系列");
        }

        this.seriesService.delete(series);

        return view;
    }

}
